package api

import (
	"net/http"

	"example.com/example/dao"
	"example.com/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configIDCardDecodeRouter(router *httprouter.Router) {
	router.GET("/idcarddecode", GetAllIDCardDecode)
	router.POST("/idcarddecode", AddIDCardDecode)
	router.GET("/idcarddecode/:argId", GetIDCardDecode)
	router.PUT("/idcarddecode/:argId", UpdateIDCardDecode)
	router.DELETE("/idcarddecode/:argId", DeleteIDCardDecode)
}

func configGinIDCardDecodeRouter(router gin.IRoutes) {
	router.GET("/idcarddecode", ConverHttprouterToGin(GetAllIDCardDecode))
	router.POST("/idcarddecode", ConverHttprouterToGin(AddIDCardDecode))
	router.GET("/idcarddecode/:argId", ConverHttprouterToGin(GetIDCardDecode))
	router.PUT("/idcarddecode/:argId", ConverHttprouterToGin(UpdateIDCardDecode))
	router.DELETE("/idcarddecode/:argId", ConverHttprouterToGin(DeleteIDCardDecode))
}

// GetAllIDCardDecode is a function to get a slice of record(s) from id_card_decode table in the custom_process database
// @Summary Get list of IDCardDecode
// @Tags IDCardDecode
// @Description GetAllIDCardDecode is a handler to get a slice of record(s) from id_card_decode table in the custom_process database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.IDCardDecode}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /idcarddecode [get]
// http "http://localhost:8080/idcarddecode?page=0&pagesize=20" X-Api-User:user123
func GetAllIDCardDecode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "id_card_decode", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllIDCardDecode(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetIDCardDecode is a function to get a single record from the id_card_decode table in the custom_process database
// @Summary Get record from table IDCardDecode by  argId
// @Tags IDCardDecode
// @ID argId
// @Description GetIDCardDecode is a function to get a single record from the id_card_decode table in the custom_process database
// @Accept  json
// @Produce  json
// @Param  argId path int64 true "id"
// @Success 200 {object} model.IDCardDecode
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /idcarddecode/{argId} [get]
// http "http://localhost:8080/idcarddecode/1" X-Api-User:user123
func GetIDCardDecode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "id_card_decode", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetIDCardDecode(ctx, argId)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddIDCardDecode add to add a single record to id_card_decode table in the custom_process database
// @Summary Add an record to id_card_decode table
// @Description add to add a single record to id_card_decode table in the custom_process database
// @Tags IDCardDecode
// @Accept  json
// @Produce  json
// @Param IDCardDecode body model.IDCardDecode true "Add IDCardDecode"
// @Success 200 {object} model.IDCardDecode
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /idcarddecode [post]
// echo '{"update_time": "2222-12-01T04:42:09.700599911+08:00","id": 23,"name": "SKCSBMahKCkMRHLORaskhLoYM","create_time": "2248-04-17T08:01:00.240026028+08:00"}' | http POST "http://localhost:8080/idcarddecode" X-Api-User:user123
func AddIDCardDecode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	idcarddecode := &model.IDCardDecode{}

	if err := readJSON(r, idcarddecode); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := idcarddecode.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	idcarddecode.Prepare()

	if err := idcarddecode.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "id_card_decode", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	idcarddecode, _, err = dao.AddIDCardDecode(ctx, idcarddecode)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, idcarddecode)
}

// UpdateIDCardDecode Update a single record from id_card_decode table in the custom_process database
// @Summary Update an record in table id_card_decode
// @Description Update a single record from id_card_decode table in the custom_process database
// @Tags IDCardDecode
// @Accept  json
// @Produce  json
// @Param  argId path int64 true "id"
// @Param  IDCardDecode body model.IDCardDecode true "Update IDCardDecode record"
// @Success 200 {object} model.IDCardDecode
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /idcarddecode/{argId} [put]
// echo '{"update_time": "2222-12-01T04:42:09.700599911+08:00","id": 23,"name": "SKCSBMahKCkMRHLORaskhLoYM","create_time": "2248-04-17T08:01:00.240026028+08:00"}' | http PUT "http://localhost:8080/idcarddecode/1"  X-Api-User:user123
func UpdateIDCardDecode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	idcarddecode := &model.IDCardDecode{}
	if err := readJSON(r, idcarddecode); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := idcarddecode.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	idcarddecode.Prepare()

	if err := idcarddecode.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "id_card_decode", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	idcarddecode, _, err = dao.UpdateIDCardDecode(ctx,
		argId,
		idcarddecode)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, idcarddecode)
}

// DeleteIDCardDecode Delete a single record from id_card_decode table in the custom_process database
// @Summary Delete a record from id_card_decode
// @Description Delete a single record from id_card_decode table in the custom_process database
// @Tags IDCardDecode
// @Accept  json
// @Produce  json
// @Param  argId path int64 true "id"
// @Success 204 {object} model.IDCardDecode
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /idcarddecode/{argId} [delete]
// http DELETE "http://localhost:8080/idcarddecode/1" X-Api-User:user123
func DeleteIDCardDecode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "id_card_decode", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteIDCardDecode(ctx, argId)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
